/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.jcjg.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hcht.xrlh.jcjg.dao.DrJgDao;
import com.hcht.xrlh.jcjg.dao.DrWtxxDao;
import com.hcht.xrlh.jcjg.entity.DrJg;
import com.hcht.xrlh.jcjg.entity.DrWtxx;
import com.hcht.xrlh.rw.dao.DrRwDao;
import com.hcht.xrlh.rw.entity.DrRw;
import com.hcht.xrlh.rwfz.dao.DrXzRwDao;
import com.hcht.xrlh.rwfz.entity.DrXzRw;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;

/**
 * 检查结果Service
 * @author 检查结果
 * @version 2016-12-23
 */
@Service
@Transactional(readOnly = true)
public class DrJgService extends CrudService<DrJgDao, DrJg> {

	@Autowired
	private DrWtxxDao drWtxxDao;
	
	@Autowired
	private DrRwDao drRwDao;
	
	@Autowired
	DrXzRwDao drXzRwDao;
	public DrJg get(String id) {
		DrJg drJg = super.get(id);
		drJg.setDrWtxxList(drWtxxDao.findList(new DrWtxx(drJg)));
		return drJg;
	}
	
	public List<DrJg> findList(DrJg drJg) {
		return super.findList(drJg);
	}
	
	public Page<DrJg> findPage(Page<DrJg> page, DrJg drJg) {
		return super.findPage(page, drJg);
	}
	
	@Transactional(readOnly = false)
	public void save(DrJg drJg) {
		super.save(drJg);
		for (DrWtxx drWtxx : drJg.getDrWtxxList()){
			if (drWtxx.getId() == null){
				continue;
			}
			if (DrWtxx.DEL_FLAG_NORMAL.equals(drWtxx.getDelFlag())){
				if (StringUtils.isBlank(drWtxx.getId())){
					drWtxx.setJgId(drJg);
					drWtxx.preInsert();
					drWtxxDao.insert(drWtxx);
				}else{
					drWtxx.preUpdate();
					drWtxxDao.update(drWtxx);
				}
			}else{
				drWtxxDao.delete(drWtxx);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(DrJg drJg) {
		super.delete(drJg);
		drWtxxDao.delete(new DrWtxx(drJg));
	}

	public String getdxNames(DrJg drJg) {
		String xds = "";
		DrRw rw = drRwDao.get(drJg.getRwId());
		DrXzRw q_dx = new DrXzRw();
		q_dx.setRwId(rw.getId());
		List<DrXzRw> dxList = drXzRwDao.findList(q_dx);
		for(DrXzRw dx : dxList){
			xds = xds + dx.getCol1()+"、";
		}
		if(xds.indexOf("、")>0){
			xds = xds.substring(0,xds.lastIndexOf("、"));
		}
		return xds;
	}
	
}